package ynu.edu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ynu.edu.dto.CommonResponse;
import ynu.edu.entity.OrderDetailet;
import ynu.edu.entity.Orders;
import ynu.edu.service.OrdersService;
import ynu.edu.service.UserService;
import ynu.edu.entity.User;
import lombok.extern.slf4j.Slf4j;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/OrdersController")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private UserService userService;

    @PostMapping("/createOrders")
    public ResponseEntity<?> createOrders(@RequestBody Orders orderData) {
        log.info("Received createOrders request with data: {}", orderData);
        try {
            // 校验必要参数
            if (orderData.getUserName() == null) {
                return ResponseEntity.badRequest().body(new CommonResponse(false, "userName不能为空"));
            }
            if (orderData.getBusinessId() == null) {
                return ResponseEntity.badRequest().body(new CommonResponse(false, "businessId不能为空"));
            }
            if (orderData.getDaId() == null) {
                return ResponseEntity.badRequest().body(new CommonResponse(false, "daId不能为空"));
            }
            if (orderData.getOrderTotal() == null) {
                return ResponseEntity.badRequest().body(new CommonResponse(false, "orderTotal不能为空"));
            }

            // 通过userName获取userId
            User user = userService.getUserByName(orderData.getUserName());
            orderData.setUserId(user.getUserId());

            Orders order = ordersService.createOrders(orderData);
            return ResponseEntity.ok(new CommonResponse(true, "创建订单成功", order));
        } catch (Exception e) {
            log.error("Failed to create order", e);
            return ResponseEntity.badRequest().body(new CommonResponse(false, e.getMessage()));
        }
    }

    @PostMapping("/listOrdersByUserId")
    public ResponseEntity<?> listOrdersByUserId(@RequestBody Orders params) {
        log.info("Received listOrdersByUserId request with userName: {}", params.getUserName());
        try {
            if (params.getUserName() == null) {
                return ResponseEntity.badRequest().body(new CommonResponse(false, "userName不能为空"));
            }
            // 通过userName获取userId
            User user = userService.getUserByName(params.getUserName());
            List<Orders> orders = ordersService.listOrdersByUserId(user.getUserId());
            return ResponseEntity.ok(new CommonResponse(true, "查询成功", orders));
        } catch (Exception e) {
            log.error("Failed to list orders", e);
            return ResponseEntity.badRequest().body(new CommonResponse(false, e.getMessage()));
        }
    }

    @PostMapping("/getOrdersById")
    public ResponseEntity<?> getOrdersById(@RequestBody Orders params) {
        log.info("Received getOrdersById request with orderId: {}", params.getOrderId());
        try {
            if (params.getOrderId() == null) {
                return ResponseEntity.badRequest().body(new CommonResponse(false, "orderId不能为空"));
            }
            Orders order = ordersService.getOrdersById(params.getOrderId());
            return ResponseEntity.ok(new CommonResponse(true, "查询成功", order));
        } catch (Exception e) {
            log.error("Failed to get order", e);
            return ResponseEntity.badRequest().body(new CommonResponse(false, e.getMessage()));
        }
    }

    @PostMapping("/getOrderDetailetByOrderId")
    public ResponseEntity<?> getOrderDetailetByOrderId(@RequestBody Orders params) {
        log.info("Received getOrderDetailetByOrderId request with orderId: {}", params.getOrderId());
        try {
            if (params.getOrderId() == null) {
                return ResponseEntity.badRequest().body(new CommonResponse(false, "orderId不能为空"));
            }
            List<OrderDetailet> details = ordersService.getOrderDetailetByOrderId(params.getOrderId());
            log.info("Found {} order details for orderId: {}", details.size(), params.getOrderId());
            return ResponseEntity.ok(new CommonResponse(true, "查询成功", details));
        } catch (Exception e) {
            log.error("Failed to get order details", e);
            return ResponseEntity.badRequest().body(new CommonResponse(false, e.getMessage()));
        }
    }

    @PostMapping("/updateOrderState")
    public ResponseEntity<?> updateOrderState(@RequestBody Orders params) {
        return ResponseEntity.ok(ordersService.updateOrderState(params.getOrderId(), params.getOrderState()));
    }
} 